package com.cypher.encryptionanddecryption.fragment.protocol;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.cypher.encryptionanddecryption.activity.BluetoothCommunicationActivity;
import com.cypher.encryptionanddecryption.bluetooth.BluetoothGattManager;
import com.cypher.encryptionanddecryption.util.KunLunProtocolUtils;
import com.cypher.encryptionanddecryption.util.Nb25ProtocolUtils;

/**
 * 协议展示区抽象类
 * @author: 古霸天
 * @date: 2020/11/20
 */
public abstract class AbstractProtocolFragment extends Fragment {
    protected BluetoothCommunicationActivity mActivity;
    /** 默认协议框架和版本为2.5协议 */
    protected String PROTOCOL_VERSION = "0025";
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mActivity = (BluetoothCommunicationActivity) context;
        if (mActivity.getProtocolId() == 2) {
            // 津燃协议
            PROTOCOL_VERSION = "0000";
//            Nb25ProtocolUtils.READ_CONTROL = "84";
//            Nb25ProtocolUtils.WRITE_CONTROL = "85";
//            Nb25ProtocolUtils.MESSAGE_NUMBER = "01";
        } else if (mActivity.getProtocolId() == 0) {
            // 2.5协议
            PROTOCOL_VERSION = "0025";
//            Nb25ProtocolUtils.READ_CONTROL = "94";
//            Nb25ProtocolUtils.WRITE_CONTROL = "95";
//            Nb25ProtocolUtils.MESSAGE_NUMBER = "00";
        }
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
    /**
     * 填充Fragment
     * @param data
     */
    abstract public void fillFragment(String data);

    /**
     * 获取读写hex码
     * @param rwType
     *      读写类型
     *      read-读数据
     *      write-写数据
     * @return
     */
    abstract public String getHexCode(String rwType);

    /**
     * 根据协议ID校验写入数据是否成功
     * @param protocolId
     * @return
     */
    public boolean checkData(String data, int protocolId) {
        boolean isSuccess = false;
        switch (protocolId) {
            case 0:
            case 2:
                isSuccess = Nb25ProtocolUtils.checkWriteDataStatus(data);
                break;
            case 1:
                isSuccess = KunLunProtocolUtils.checkWriteDataStatus(data);
                break;
        }
        return isSuccess;
    }

    /**
     * 处理特殊情况
     * @param gattManager
     *      蓝牙通信管理类
     * @param rwType
     *      读写类型
     * @return
     *      处理特殊情况失败返回false，反之返回true
     */
    public boolean specialCases(BluetoothGattManager gattManager, String rwType) {
        return true;
    }
}
